<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xx="24">
        <el-form
          class="login-form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>欢迎学习vue3</h1>
          <h2>来了</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loadinglogin"
              type="primary"
              size="default"
              class="login-btn"
              @click="Login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { ElNotification } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
//引入时间
import { getTime } from '@/utils/time'
//拿到组件的实列
let loginForms = ref()
//获取路由
let $router = useRouter()
//引入小仓库
import useUserStore from '@/store/modules/user'
let loginForm = reactive({ username: 'admin', password: '111111' })
let userStore = useUserStore()
//按钮加载的效果
let loadinglogin = ref(false)
//获取路由对象
let $route = useRoute()
//
// 登录
const Login = async () => {
  await loginForms.value.validate()
  loadinglogin.value = true
  try {
    await userStore.userLogin(loginForm)
    let redirect: any = $route.query.redirect
    //登录成功跳转到home页
    $router.push({ path: redirect || '/' })
    //登录成功提示信息
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI${getTime()}好`,
    })
    //登录成功后效果消失
    loadinglogin.value = false
  } catch (error) {
    loadinglogin.value = false
    //登录失败提示信息
    ElNotification({
      type: 'error',
      message: error.message,
    })
  }
}
//自定义表单校验函数
const validatorUserName = (rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('你的账号长度至少五为'))
  }
}
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('你的密码长度至少六为'))
  }
}
//自定义表单校验
const rules = {
  username: [{ trigger: 'change', validator: validatorUserName }],
  password: [{ trigger: 'change', validator: validatorPassword }],
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login-form {
    position: relative;
    width: 80%;
    top: 35vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;
    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
